package com.tanhua.dubbo.api.impl;

import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.RecommendUserApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service(timeout = 1000000)
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    // db.recommend_user.find({userId:1}).sort({score: -1}).limit(1)
    public RecommendUser queryWithMaxScore(Long userId) {
        // 创建查询对象，指定查询条件
        Query query = new Query(Criteria.where("userId").is(userId));
        // 指定排序参数
        query.with(Sort.by(Sort.Order.desc("score")));
        // 指定查询第一条数据
        query.limit(1);

        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    @Override
    // db.recommend_user.find({userId:1}).sort({score: -1}).limit(10).skip(0)
    public PageResult queryRecommendation(Integer page, Integer pagesize, Long userId) {
        // 创建查询对象，指定查询条件
        Query query = new Query(Criteria.where("userId").is(userId));
        // 指定排序参数
        query.with(Sort.by(Sort.Order.desc("score")));
        // 指定分页参数
        query.limit(pagesize).skip((page - 1) * pagesize);
        // 查询当前页的数据
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        // 查询总条数
        long count = mongoTemplate.count(query, RecommendUser.class);

        return new PageResult(page, pagesize, (int) count, recommendUserList);
    }

    @Override
    public long queryScore(Long userId, Long id) {
        Query query = new Query(Criteria.where("userId").is(userId).and("recommendUserId").is(id));
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        if (recommendUserList != null && recommendUserList.size() > 0) {
            return recommendUserList.get(0).getScore().longValue();
        }
        return 80L;
    }

    /**
     * 根据用户Id，返回num个随件推荐用户ID
     */
    @Override
    public List<Long> queryRandomList(Long userId, Integer num) {
        Query query = new Query(Criteria.where("userId").is(userId));
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        List<Long> recommendUserIds = new ArrayList<>();
        if(recommendUserList.size()<num){
            num = recommendUserList.size();
        }
        for (int i=0;i<num;i++){
            RecommendUser recommendUser = recommendUserList.get(new Random().nextInt(recommendUserList.size()));
            recommendUserIds.add(recommendUser.getRecommendUserId());
        }
        return recommendUserIds;
    }

    /**
     * 根据用户Id，和推荐用户Id，删除数据
     */
    @Override
    public void findRecommend(Long userId, Long recommendUserId) {
        Query query = new Query(Criteria.where("userId").is(userId)
                .and("recommendUserId").is(recommendUserId));
        mongoTemplate.remove(query,RecommendUser.class);
    }
}